package com.atguigu.tingshu.album.service.impl;

import com.atguigu.tingshu.album.mapper.*;
import com.atguigu.tingshu.album.service.BaseCategoryService;
import com.atguigu.tingshu.common.execption.GuiguException;
import com.atguigu.tingshu.model.album.*;
import com.atguigu.tingshu.model.base.BaseEntity;
import com.atguigu.tingshu.vo.category.CategoryVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@SuppressWarnings({"unchecked", "rawtypes"})
public class BaseCategoryServiceImpl extends ServiceImpl<BaseCategory1Mapper, BaseCategory1> implements BaseCategoryService {

    @Autowired
    private BaseCategory1Mapper baseCategory1Mapper;

    @Autowired
    private BaseCategory2Mapper baseCategory2Mapper;

    @Autowired
    private BaseCategory3Mapper baseCategory3Mapper;

    @Autowired
    private BaseCategoryViewMapper baseCategoryViewMapper;
    @Autowired
    private BaseAttributeMapper baseAttributeMapper;


    // todo：优化
    // 1. 使用递归
    // 2. sql层面（MyBatis持久层框架）
    @Override
    public List<CategoryVo> getBaseCategoryList() {
        return baseCategory1Mapper.getBaseCategoryList();
    }

    @Override
    public CategoryVo getBaseCategoryList(Long c1Id) {
        return baseCategory1Mapper.getBaseCategoryListByc1Id(c1Id);
    }

    @Override
    public List<BaseAttribute> findAttribute(Long category1Id) {
        return baseAttributeMapper.findAttribute(category1Id);
    }

    @Override
    public BaseCategoryView getAlbumCategory(Long albumId) {
        return baseCategoryViewMapper.getAlbumCategory(albumId);
    }

    @Override
    public List<BaseCategory3> findTopBaseCategory3(Long c1Id) {

        //根据一级分类id查询出所有的二级分类
        List<BaseCategory2> baseCategory2s = baseCategory2Mapper.selectList(
                new LambdaQueryWrapper<BaseCategory2>()
                        .eq(BaseCategory2::getCategory1Id, c1Id)
                        .eq(BaseCategory2::getIsDeleted, 0));
        if (CollectionUtils.isEmpty(baseCategory2s)) {
            throw new GuiguException(2010, "该一级分类下没有二级分类集合");
        }
        List<Long> c2Ids = baseCategory2s.stream().map(BaseEntity::getId).toList();

        // 根据二级分类id查询出所有的三级分类
        return baseCategory3Mapper.selectList(new LambdaQueryWrapper<BaseCategory3>()
                .in(BaseCategory3::getCategory2Id, c2Ids)
                .eq(BaseCategory3::getIsDeleted, 0)
                .eq(BaseCategory3::getIsTop, 1)
                .last("limit 7"));
    }

    @Override
    public List<Long> getAllCategory1Id() {
        List<BaseCategory1> baseCategory1s = baseCategory1Mapper.selectList(null);
        return baseCategory1s.stream().map(BaseEntity::getId).toList();
    }



  /*  @Override
    public List<CategoryVo> getBaseCategoryList() {
        ArrayList<CategoryVo> result = new ArrayList<>();
        // 1.查询所有的分类信息
        List<BaseCategoryView> baseCategoryViews = baseCategoryViewMapper.selectList(null);
        // 2.封装数据
        Map<Long, List<BaseCategoryView>> category1IdAndValue = baseCategoryViews.stream()
                .collect(Collectors.groupingBy(BaseCategoryView::getCategory1Id));
        for (Map.Entry<Long, List<BaseCategoryView>> entry : category1IdAndValue.entrySet()) {
            // 1. 定义一个一级分类
            CategoryVo category1Vo = new CategoryVo();
            // 给一级分类id赋值
            category1Vo.setCategoryId(entry.getKey());
            // 给一级分类名字赋值
            category1Vo.setCategoryName(entry.getValue().get(0).getCategory1Name());
            Map<Long, List<BaseCategoryView>> category2IdAndValue = entry.getValue()
                    .stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory2Id));
            ArrayList<CategoryVo> category1Child = new ArrayList<>();
            for (Map.Entry<Long, List<BaseCategoryView>> listEntry : category2IdAndValue.entrySet()) {
                // 2. 定义二级分类
                CategoryVo category2Vo = new CategoryVo();
                // 给二级分类id赋值
                category2Vo.setCategoryId(listEntry.getKey());
                // 给二级分类名字赋值
                category2Vo.setCategoryName(listEntry.getValue().get(0).getCategory2Name());
                Map<Long, List<BaseCategoryView>> category3IdAndValue = listEntry.getValue()
                        .stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory3Id));
                ArrayList<CategoryVo> category2Child = new ArrayList<>();
                for (Map.Entry<Long, List<BaseCategoryView>> longListEntry : category3IdAndValue.entrySet()) {
                    // 3. 定义三级分类
                    CategoryVo categoryVo = new CategoryVo();
                    // 给三级分类id赋值
                    categoryVo.setCategoryId(longListEntry.getKey());
                    // 给三级分类名字赋值
                    categoryVo.setCategoryName(longListEntry.getValue().get(0).getCategory3Name());
                    // 给三级分类的孩子赋值
                    categoryVo.setCategoryChild(null);
                    category2Child.add(categoryVo);
                }
                // 给二级分类的孩子赋值
                category2Vo.setCategoryChild(category2Child);
                category1Child.add(category2Vo);
            }
            // 给一级分类的孩子赋值
            category1Vo.setCategoryChild(category1Child);
            result.add(category1Vo); // 将一级分类放入到结果集中
        }
        return result;
    }*/
}
